package edu.cuit.zhuyimeng.ticket.controller;

import edu.cuit.zhuyimeng.framework.common.result.CommonResult;
import edu.cuit.zhuyimeng.ticket.service.IFlightService;
import edu.cuit.zhuyimeng.ticket.service.ITicketSessionManager;
import edu.cuit.zhuyimeng.ticket.service.bo.TicketDispatchSession;
import edu.cuit.zhuyimeng.ticket.service.impl.TicketSessionManager;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Tag(name = "测试")
@RequestMapping("test")
@RequiredArgsConstructor
public class TestController {

    private final ITicketSessionManager ticketSessionManager;
    private final IFlightService flightService;

    @GetMapping("/testBuyTicket")
    @Operation(summary = "测试买票")
    public CommonResult<Void> testBuyTicket(@RequestParam("flightId") Long flightId) {
        boolean result = ticketSessionManager.buyTicket(flightService.queryFlightInfo(flightId));
        return result ? CommonResult.success() : CommonResult.error("买票失败");
    }

    @GetMapping("/testGetTicketAmount")
    @Operation(summary = "查看票数临时接口")
    public CommonResult<Integer> tempGetTicketAmount(@RequestParam("flightId") Long flightId) {
        TicketDispatchSession ticketDispatchSession = ((TicketSessionManager) ticketSessionManager).getTicketDispatchSession(flightId);
        return ticketDispatchSession == null ? CommonResult.error("该航班没有订票") : CommonResult.success(ticketDispatchSession.getTicketAmount());
    }

}
